package com.luckku.boot.controller;


import com.luckku.boot.common.api.ApiResult;
import com.luckku.boot.entity.Admin;
import com.luckku.boot.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Luckku
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * /admin/register
     * 管理员注册
     *
     * @param admin Admin
     * @return ApiResult<Map<String,Object>>
     */
    @PostMapping(value = "/register")
    public ApiResult<Map<String,Object>> register(@Valid @RequestBody Admin admin) {
        admin = adminService.executeRegister(admin);
        if (ObjectUtils.isEmpty(admin)) {
            return ApiResult.failed("账号注册失败");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("admin",admin);
        return ApiResult.success(map);
    }

    /**
     * /admin/login
     * 管理员登录
     *
     * @param admin Admin
     */
    @PostMapping(value = "/login")
    public ApiResult<Map<String, String>> login(@Valid @RequestBody Admin admin) {
        Map<String, String> map = adminService.executeLogin(admin);
        if (!map.isEmpty()) {
            return ApiResult.success(map, "登录成功");
        }
        return ApiResult.failed("账号或密码错误");
    }
}

